﻿using System;
using System.Collections.Generic;
using System.Text;
using TarreOA.TarreOAModels;
using TarreOA.TarreOADAL;
using System.IO;

namespace TarreOA.TarreOABLL
{
    public class DiaryFileUploadMappingtbService
    {
        //保存日记附件信息
        public int saveDiaryAffixFile(string fileName, string filePath, DateTime uploadTime, int diaryId)
        {
            DiaryFileUploadMappingtbDAO diaryFileUploadMappingtbDAO = new DiaryFileUploadMappingtbDAO();
            return diaryFileUploadMappingtbDAO.saveAffixFileInfo(fileName, filePath, uploadTime, diaryId);
        }

        //根据日记编号查找附件信息
        public List<DiaryFileUploadMappingtb> findByDiaryId(int did)
        {
            DiaryFileUploadMappingtbDAO diaryFileUploadMappingtbDAO = new DiaryFileUploadMappingtbDAO();
            return diaryFileUploadMappingtbDAO.findByDiaryId(did);
        }

        //根据日记编号删除附件信息并删除相应文件
        public bool delAffixFileInfoByDiaryId(string[] dids)
        {
            DiaryFileUploadMappingtbDAO diaryFileUploadMappingtbDAO = new DiaryFileUploadMappingtbDAO();
            for (int i = 0; i < dids.Length; i++)
            {
                int did = int.Parse(dids[i]);
                List<DiaryFileUploadMappingtb> list = diaryFileUploadMappingtbDAO.findByDiaryId(did);
                if (list != null && list.Count > 0)
                {
                    foreach (DiaryFileUploadMappingtb diaryFileUploadMapingtb in list)
                    {
                        File.Delete(diaryFileUploadMapingtb.FilePath);
                    }
                    diaryFileUploadMappingtbDAO.delAffixFileInfoByDiaryId(did);
                }
            }
            return true;
        }
    }
}
